package edu.towson.cosc.cosc455.sliao1.markdowncompiler;
import java.util.*;
import java.io.*;

public class Compiler {
	public static Scanner reader;
    public static BufferedWriter writer;
	public static String text = "";
    static String token;
    
    public static Stack<String> validTokens = new Stack<String>();
    public static MysyntaxAnalyzer syntax = new MysyntaxAnalyzer(); 
    public static MysemanticAnalyzer semantic = new MysemanticAnalyzer();
    
    
    public static void main(String[] args) throws IOException, CompilerException 
    {
    	// 1. check to make sure you have the correct arguments
    	// 2. check to make sure the file has an .mkd extension --> endsWith(".mkd");
    	// 3. initializations
    	
    	//read in the text file
    	boolean fileFound = false;
    	
    	Scanner scan = new Scanner(System.in);
    	System.out.println("Enter the directory to the file");
    	String fileName = scan.nextLine();
    	String extension = fileName.substring((fileName.length()-4));
    	if(!".mkd".equals(extension))
    	{
    		System.out.println("The file extension is not .mkd");
    	}	
    	else	
    	try{
            reader = new Scanner(new File(fileName));
            String line = "";
            while(reader.hasNextLine())
            {
            	line = reader.nextLine();
            	text = text + line;
            }            
            fileFound = true;
            reader.close();
    	}
    	catch(IOException e)
    	{
    		fileFound = false;
    		System.out.println("File was not found.");
    		reader.close();
    		e.printStackTrace();
    	}  	
    	//if the file is found, proceed with compiler functions
    	if(fileFound){
    		token="";
        	// 4. call the lexical analyzer to get the first token set
    			//in the syntax analyzer
            // 5. call the start state of the parser, i.e., document()
    		try{
    			syntax.document();
    		}
    		catch(CompilerException e){
    			e.printStackTrace();
    		}
            // ... lexer and parser will then do its thing
            // 6. call the semantic analyzer
    		// 7. write the output file to html
    		String outPutFile = fileName;
        	outPutFile = outPutFile.substring(0, outPutFile.length() - 4) + ".html";
    		writer= new BufferedWriter(new FileWriter(outPutFile));
    		semantic.convert();         	
            // 8. clean up (closing file...etc)
    		writer.close();
    		reader.close();
    }
  }
}
    	
    
    
